/**
 * 
 */
package it.izsto.fe.bean;

import it.izsto.bo.FiltriBean;
import it.izsto.constants.Constants;
import it.izsto.dto.sig4.VControlloAccessi;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.ldap.LdapContext;


/**
 * @author terribilio.gi
 * 
 */
@ManagedBean(name = "homePortaleBean")
@SessionScoped
public class HomePortaleBean extends BaseBean {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;

	private FiltriBean filtri;
	
	private String username;
	private String password;
	
	private String newUser;

	private List<VControlloAccessi> list;

	public String searchResult() {
		setList(getServiceBean().getRiepilogoList(filtri));
		return "";

	}

	@PostConstruct
	public void initialize() {
		log.debug("Init");
		filtri = new FiltriBean();
		list = new ArrayList<VControlloAccessi>();
	}
	
	public void doOnLoad() {
		
		try{
		    LdapContext conn = ActiveDirectory.getConnection(getUsername(), getPassword(),"izsto.it","srvmail");
		    //ActiveDirectory.getUser("bob", conn).changePassword("password", "NewPassword!", true, conn);
		    conn.close();
		    System.out.println("Success!");
		}
		catch(Exception e){
		    //Failed to authenticate user or change password...
		    e.printStackTrace();
		}
		
		searchResult();
	}

	public String vaiDettaglio(VControlloAccessi item) {
		// getDettaglioBean().setItem(item);
		// getDettaglioBean().createPieModel();
		DettaglioBean db = (DettaglioBean) createBean("dettaglioBean");
		db.setItem(item);
		//db.createPieModel();
		//faces-redirect=true cambia il nome della pagina di destinazione
		return Constants.PageID.DettaglioID + "?faces-redirect=true";
	}
	
	public String inserisciUtente() {
		try {
			getServiceBean().inserisciUtente(newUser);
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Inserimento utente effettuato con successo",  "Your message") );
			searchResult();
		} catch(Exception e) {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), "Your message") );
		}
		return "";
	}

	public List<VControlloAccessi> getList() {
		return list;
	}

	public void setList(List<VControlloAccessi> list) {
		this.list = list;
	}

	public FiltriBean getFiltri() {
		return filtri;
	}

	public void setFiltri(FiltriBean filtri) {
		this.filtri = filtri;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	public String getNewUser() {
		return newUser;
	}

	public void setNewUser(String newUser) {
		this.newUser = newUser;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
}